<?php

// $Id$


require_once($WSC->get_site_inc_dir().'modules/admin_panel/site_admin_web_page_wed_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tree_menu_ml/tree_menu_item_ml_dbc.class.php');


class Menu_Item_Delete_WP extends Site_Admin_Web_Page_WED_UA {
	public function init() {
		parent::init();

		$v = $this->create_view();
		$this->add_view($v);

		$v2 = new Site_Admin_Web_Page_View($this, 'not_exist');
		$this->add_view($v2);


		$prefix_event = new Web_Event_Simple('prefix', Tangra_Parameter_Method::GET, 'prefix');

		$delete_event = new Web_Event_Simple_Int('delete', Tangra_Parameter_Method::GET, 'del');
		$delete_comp = new Web_Event_Composite('delete_comp');
		$delete_comp->add_wes($delete_event);
		$delete_comp->add_wes($prefix_event);
		$this->wed->add_wed_event_action_pair($delete_comp, 'delete_event');

		$delete_confirmed_event = new Web_Event_Simple_Int('delete_confirmed', Tangra_Parameter_Method::GET, 'delete_confirmed');
		$delete_confirmed_comp = new Web_Event_Composite('delete_confirmed_comp');
		$delete_confirmed_comp->add_wes($delete_confirmed_event);
		$delete_confirmed_comp->add_wes($prefix_event);
		$this->wed->add_wed_event_action_pair($delete_confirmed_comp, 'delete_confirmed_event');

	}


	protected function delete_event($params) {
		$id = $params['delete'];

		$menu_item = new Tree_Menu_Item_ML_DBC($params['prefix'].'menu', 1);
		$dbc = $this->get_dbc();
		if ($menu_item->load_by_id($dbc, $id)) {

			$this->export('label', $menu_item->get_label());
			$this->export('menu_item_id', $menu_item->get_id());

			$this->export('prefix', $params['prefix']);

			$view = $this->get_view('default');

		} else {
			$this->export('menu_item_id', $id);
			$view = $this->get_view('not_exists');

		}


		return $view;
	}

//TODO - confirm that it is working
	protected function delete_confirmed_event($params) {
		$id = $params['delete_confirmed'];

		$dbc = $this->get_dbc();

		try {
			$dbc->start_trans();
			Tree_Menu_Item_ML_DBC::delete_cascade($dbc, $id, $params['prefix']);
			$dbc->complete_trans();
		} catch (Exception $e) {
			$dbc->fail_trans();
			$dbc->complete_trans();
			throw $e;
		}

		$context = $this->get__context();
		$rc = new Redirect_Composer_Local($this->get__context(), 'admin/menu/menu_manage.php');
		$rc->add_param_pair('prefix', $params['prefix']);
		$redir_view = new Just_Headers_View();
		$redir_view->add_http_header($rc->get_location($context));

		return $redir_view;
	}


	protected function default_event() {
		throw new Tangra_User_Exception('Unexpected parameters.');
	}
}

